package com.qf.controller;


import com.qf.entity.AppCategory;
import com.qf.enums.AppEnum;
import com.qf.service.AppCategoryService;
import com.qf.util.ResultVOUtil;
import com.qf.vo.EasyUITreeVO;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
@RequestMapping("/backend/app")
public class BackendAppController {

    @Autowired
    private AppCategoryService appCategoryService;

    @GetMapping("/index")
    public String index(){
        return "backend/app/index";
    }
    //
    @GetMapping("/category-manager-ui")
    public String categoryManagerUI(){
        return "backend/app/category-manager";
    }

    //查询app分类信息（节点树）
    @GetMapping("/appCategory")
    @ResponseBody
    public List<EasyUITreeVO> appCategory(@RequestParam(defaultValue = "0")Long id){

        List<EasyUITreeVO> easyUITreeVOList = appCategoryService.findTreeVOByParentId(id);

        return easyUITreeVOList;
    }

    //添加APP分类信息
    @PostMapping("/appCategory-add")
    @ResponseBody
    public ResultVO appCategoryAdd(Long parentId,String categoryName){
        if (parentId==null || StringUtils.isEmpty(categoryName)){
            log.info("【添加分类信息】 接收参数异常！parentId={} categoryName={}",parentId,categoryName);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR);
        }

        AppCategory appCategory = new AppCategory();
        appCategory.setParentId(parentId);
        appCategory.setCategoryName(categoryName);

        appCategoryService.addParentIdAndCategoryName(appCategory);

        return ResultVOUtil.ok(appCategory.getId());
    }

    //修改app分类名称
    @PostMapping("/appCategory-rename")
    @ResponseBody
    public ResultVO appCategoryRename(Long id,String categoryName){
        if (id==null || StringUtils.isEmpty(categoryName)){
            log.info("【修改分类信息】 接收参数异常！id={} categoryName={}",id,categoryName);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR);
        }

        AppCategory appCategory = new AppCategory();
        appCategory.setId(id);
        appCategory.setCategoryName(categoryName);

        appCategoryService.updateById(appCategory);

        return ResultVOUtil.ok();
    }

    //
    @PostMapping("/appCategory-delete")
    @ResponseBody
    public ResultVO appCategoryDelete(Long id){
        if (id==null ){
            log.info("【删除分类信息】 接收参数异常！id={}",id);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR);
        }

        appCategoryService.deleteAllById(id);

        return ResultVOUtil.ok();
    }


}
